﻿//使用方法
//using Common.HelpersXMLHelper;
//using System.Collections.Generic;
//using System.Xml;
//using System;
//using XMLHelper;

////创建XML文件帮助类实例
//XMLFileHelper xml = new XMLFileHelper(@"D:\test.xml");

////添加节点
//Dictionary<string, string> attributes = new Dictionary<string, string>();
//attributes.Add("id", "1");
//xml.Add("/root", "person", "Tom", attributes);

////删除节点
//xml.Delete("/root", "person");

////修改节点
//Dictionary<string, string> newAttributes = new Dictionary<string, string>();
//newAttributes.Add("id", "2");
//xml.Modify("/root", "person", "Jerry", newAttributes);

////查询节点
//XmlNodeList result = xml.Search("/root/person");
//foreach (XmlNode xn in result)
//{
//    Console.WriteLine(xn.InnerText);
//}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Common.HelpersXMLHelper
{

    public class XMLFileHelper
    {
        private XmlDocument _xmlDoc;
        private string _xmlFilePath;

        public XMLFileHelper(string filePath)
        {
            _xmlDoc = new XmlDocument();
            _xmlFilePath = filePath;
            CreateXmlFile(filePath);
            _xmlDoc.Load(filePath);
        }
        public bool CreateXmlFile(string filePath) // 创建XML文件
        {
            if (File.Exists(filePath)) return true;
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                XmlElement root = doc.CreateElement("Root");
                doc.AppendChild(root);
                doc.Save(filePath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        //添加
        public void Add(string xmlNodePath, string nodeName, string innerText, Dictionary<string, string> attributes = null)
        {
            
            XmlNode root = _xmlDoc.SelectSingleNode(xmlNodePath);
            XmlElement xe = _xmlDoc.CreateElement(nodeName);
            xe.InnerText = innerText;
            if (attributes != null)
            {
                foreach (var attribute in attributes)
                {
                    var attr = _xmlDoc.CreateAttribute(attribute.Key);
                    attr.Value = attribute.Value;
                    xe.Attributes.Append(attr);
                }
            }
            root.AppendChild(xe);
            _xmlDoc.Save(_xmlFilePath);
        }

        //删除
        public void Delete(string xmlNodePath, string nodeName)
        {
            XmlNodeList nodeList = _xmlDoc.SelectSingleNode(xmlNodePath).ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if (xn.Name == nodeName)
                {
                    _xmlDoc.SelectSingleNode(xmlNodePath).RemoveChild(xn);
                    break;
                }
            }
            _xmlDoc.Save(_xmlFilePath);
        }

        //修改
        public void Modify(string xmlNodePath, string nodeName, string innerText, Dictionary<string, string> attributes = null)
        {
            XmlNodeList nodeList = _xmlDoc.SelectSingleNode(xmlNodePath).ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if (xn.Name == nodeName)
                {
                    xn.InnerText = innerText;
                    if (attributes != null)
                    {
                        xn.Attributes.RemoveAll();
                        foreach (var attribute in attributes)
                        {
                            var attr = _xmlDoc.CreateAttribute(attribute.Key);
                            attr.Value = attribute.Value;
                            xn.Attributes.Append(attr);
                        }
                    }
                    break;
                }
            }
            _xmlDoc.Save(_xmlFilePath);
        }

        //查询
        public XmlNodeList Search(string xmlNodePath)
        {
            XmlNodeList nodeList = _xmlDoc.SelectNodes(xmlNodePath);
            return nodeList;
        }
    }
}
